﻿using Lazy.Captcha.Core;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace B.S.BaseData.WriteAPI.Controllers
{
	/// <summary>
	/// 验证码控制器
	/// </summary>
	[Route("api/[controller]/[action]")]
	[ApiController]
	public class CodeController : ControllerBase
	{

		private readonly ICaptcha captcha;

		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name="captcha"></param>
		public CodeController(ICaptcha captcha)
		{
			this.captcha = captcha;
		}

		/// <summary>
		/// 获取验证码
		/// </summary>
		/// <param name="str"></param>
		/// <returns></returns>
		[HttpGet]
		public IActionResult Captcha(string str)
		{
			//生成验证码
			var info = captcha.Generate(str);

			var stream = new MemoryStream(info.Bytes);
			return File(stream, "image/gif");
		}

		/// <summary>
		/// 验证码验证
		/// </summary>
		/// <param name="code"></param>
		/// <param name="str"></param>
		/// <returns></returns>
		[HttpGet]
		public bool Validate(string code, string str)
		{
			return captcha.Validate(code, str);
		}


	}
}
